###REPLICATION CODE FOR CAVALIERI KUSHI RUSSO 2025 - ITPSR/RISP

#install packages

library(readr)        
library(tidyverse)
library(grid)
library(gridExtra)
library(stringr)
library(ggforce)
library(psych)                    
library(datasets)
library(ggpubr)
library(ggExtra)
library(openxlsx)
library(lubridate)
library(janitor)
library(readxl)
library(cowplot)
library(zoo)

###Figure 1. Events considered one of the Most Important Issues by citizens (2005–2022).####

#MII at national level#
MII_IT_country <- read_delim("EurobarometerMII.csv", 
                             delim = ";", escape_double = FALSE, col_types = cols(Crime = col_number(), 
                                                                                  `The economic situation` = col_number(), 
                                                                                  `Rising prices / inflation / cost of living` = col_number(), 
                                                                                  Taxation = col_number(), Unemployment = col_number(), 
                                                                                  Terrorism = col_number(), Housing = col_number(), 
                                                                                  `Government debt` = col_number(), 
                                                                                  Immigration = col_number(), Health = col_number(), 
                                                                                  `The education system` = col_number(), 
                                                                                  Pensions = col_number(), `The environment and climate change` = col_number(), 
                                                                                  `Energy supply` = col_number(), `The international situation` = col_number(), 
                                                                                  `Other (SPONTANEOUS)` = col_number(), 
                                                                                  `None (SPONTANEOUS)` = col_number(), 
                                                                                  `Don't know` = col_number()), trim_ws = TRUE)

#replace missing values with 0

MII_IT_country[is.na(MII_IT_country)] <- 0

#transfor df into long format

MII_country_long <- MII_IT_country %>% 
  gather(variable, value, -date)


#create the figure

MII_country_long %>% filter(date < "2023-Q1") %>% 
  filter(!(variable %in% c("Crime", "Taxation", "Terrorism", "Government debt",
                           "The education system", "Pensions", "The international situation",
                           "Energy supply", "Don't know", "Other (SPONTANEOUS)", "None (SPONTANEOUS)"))) %>% 
  ggplot(aes(x = date, y = value, group = variable, color = variable)) +
  geom_line(size = 0.6) +
  labs(x = "Semesters",
       y = "MII facing your country") +
  scale_y_continuous(limits = c(0, 70)) +
  theme_classic() +
  theme(panel.grid.major.x = element_line(),
        panel.grid.minor.y = element_blank(),
        axis.text.y = element_text(size = 12),
        axis.text.x = element_text(size = 12, angle = 90, hjust = 1),
        axis.title.y = element_text(margin = unit(c(0,3,0,0), "mm"), size = 14),
        axis.title.x = element_blank(),
        legend.text = element_text(size = 12),
        legend.position = "bottom",
        legend.title = element_blank()) +
  guides(color = guide_legend(nrow = 2, byrow = TRUE)) 




###Table 2. Summary statistics of the dependent variables between semesters (2005–2022).####

RISP_QT_crises <- read_csv("QuestionTime.csv")
RISP_WRITTEN_crises <- read_csv("WrittenQuestions.csv")

#the category economy is composed by macro economics and labour so use the same label for both
RISP_QT_crises$cap_macro[RISP_QT_crises$cap_macro == 5] <- 1

#then sum those with the same label
RISP_QT_crises <- RISP_QT_crises %>% group_by(cap_macro, semester) %>% 
  mutate(nQT.categSUM = sum(nQT.categ))

#now delete nQT.categ so I can remove duplicates
RISP_QT_crises <- RISP_QT_crises[, -3]

#remove duplicates
RISP_QT_crises <- RISP_QT_crises[!duplicated(RISP_QT_crises), ]


#same with written questions

#the category economy is composed by macro economics and labour so use the same label for both
RISP_WRITTEN_crises$cap_macro[RISP_WRITTEN_crises$cap_macro == 5] <- 1

#then sum those with the same label
RISP_WRITTEN_crises <- RISP_WRITTEN_crises %>% group_by(cap_macro, semester) %>% 
  mutate(nWRITTEN.categSUM = sum(nWRITTEN.categ))

#now delete nQT.categ so I can remove duplicates
RISP_WRITTEN_crises <- RISP_WRITTEN_crises[, -3]

#remove duplicates
RISP_WRITTEN_crises <- RISP_WRITTEN_crises[!duplicated(RISP_WRITTEN_crises), ]


#create a function for percentage change
growth <- function(x)(x/lag(x)-1)

#apply the function to number of oral questions
RISP_QT_crises <- RISP_QT_crises %>% group_by(cap_macro) %>% 
  mutate(perChange = growth(nQT.categSUM)*100)

#apply the function to number of written questions
RISP_WRITTEN_crises <- RISP_WRITTEN_crises %>% group_by(cap_macro) %>% 
  mutate(perChange = growth(nWRITTEN.categSUM)*100)


#create the function to change Not A Number change as 0

is.nan.data.frame <- function(x)
  do.call(cbind, lapply(x, is.nan))

#create the function to change Infinite change as 0
is.infinite.data.frame <- function(x)
  do.call(cbind, lapply(x, is.infinite))


#when the result is NaN or Inf change as 0
RISP_QT_crises[is.nan(RISP_QT_crises)] <- 0
RISP_QT_crises[is.infinite(RISP_QT_crises)] <- 0
RISP_WRITTEN_crises[is.infinite(RISP_WRITTEN_crises)] <- 0
RISP_WRITTEN_crises[is.nan(RISP_WRITTEN_crises)] <- 0


#Frequency distribution of each category in oral and written questions

#need all the observations, not only semesters

#first merge dfs -> need to rename variables
RISP_WRITTEN_crises <- rename(RISP_WRITTEN_crises, TOTsemesterWRITTEN = TOTsemester)
RISP_WRITTEN_crises <- rename(RISP_WRITTEN_crises, perChangeWRITTEN = perChange)

RISP_QT_crises <- rename(RISP_QT_crises, TOTsemesterQT = TOTsemester)
RISP_QT_crises <- rename(RISP_QT_crises, perChangeQT = perChange)

#now merge dfs of oral and written questions
bothPERchange <- full_join(RISP_QT_crises, RISP_WRITTEN_crises)

#filter per date
bothPERchange <- bothPERchange %>% filter(semester > "2004.2")

#put the df in long format
bothLONG <- bothPERchange %>% dplyr::select(cap_macro, semester, perChangeQT, perChangeWRITTEN) %>% 
  gather(variable, value, -semester, -cap_macro) %>% 
  filter(semester > "2004.2")


#summary statistics of each category
bothPERchange %>% 
  filter(cap_macro == "9") %>% 
  summary(min = min(perChangeQT),
          mean = mean(perChangeQT),
          median = median(perChangeQT),
          max = max(perChangeQT))


bothPERchange %>% 
  filter(cap_macro == "3") %>% 
  summary(min = min(perChangeQT),
          mean = mean(perChangeQT),
          median = median(perChangeQT),
          max = max(perChangeQT))


bothPERchange %>% 
  filter(cap_macro == "1") %>% 
  summary(min = min(perChangeQT),
          mean = mean(perChangeQT),
          median = median(perChangeQT),
          max = max(perChangeQT))


#calculate kurtosis and L-kurtosis
library(e1071)                      #for kurtosis
library(Lmoments)                   #for L-kurtosis

kurtEcon <- bothPERchange %>% filter(cap_macro == 1)

kurtHealth <- bothPERchange %>% filter(cap_macro == 3)

kurtImmig <- bothPERchange %>% filter(cap_macro == 9)


kurtosis(kurtEcon$perChangeQT, na.rm = TRUE)              

kurtosis(kurtEcon$perChangeWRITTEN, na.rm = TRUE)             

Lcoefs(na.omit(kurtEcon$perChangeQT), rmax = 4)[4]      

Lcoefs(na.omit(kurtEcon$perChangeWRITTEN), rmax = 4)[4]      



kurtosis(kurtHealth$perChangeQT, na.rm = TRUE)

kurtosis(kurtHealth$perChangeWRITTEN, na.rm = TRUE)              

Lcoefs(na.omit(kurtHealth$perChangeQT), rmax = 4)[4]      

Lcoefs(na.omit(kurtHealth$perChangeWRITTEN), rmax = 4)[4]      



kurtosis(kurtImmig$perChangeQT, na.rm = TRUE)             

kurtosis(kurtImmig$perChangeWRITTEN, na.rm = TRUE)              

Lcoefs(na.omit(kurtImmig$perChangeQT), rmax = 4)[4]      

Lcoefs(na.omit(kurtImmig$perChangeWRITTEN), rmax = 4)[4]      



###Figure 2. Frequency distribution of changes of attention towards Economy, Health, and Immigration (2005–2022).####

bothLONG$cap_macro[bothLONG$cap_macro == "1"] <- "Economy"
bothLONG$cap_macro[bothLONG$cap_macro == "3"] <- "Health"
bothLONG$cap_macro[bothLONG$cap_macro == "9"] <- "Immigration"


bothLONG %>% filter(cap_macro %in% c("Economy", "Health", "Immigration")) %>% 
  ggplot(aes(x = value, group = variable, fill = variable)) +
  #geom_density(alpha = .75) +
  geom_histogram(bins = 60, alpha = .75) +
  scale_fill_manual(breaks = c("perChangeQT", "perChangeWRITTEN"),
                    labels = c("question time", "written questions"),
                    values = c("#5e3c99", "#e66101")) +
  xlab("Percentage change between semesters") +
  ylab("Count") +
  theme_classic() +
  theme(panel.grid = element_blank(),
        axis.text.y = element_text(size = 12),
        axis.text.x = element_text(size = 12),
        axis.title.y = element_text(size = 14, margin = unit(c(0,3,0,0), "mm")),
        axis.title.x = element_text(size = 14, margin = unit(c(5,0,0,0), "mm")),
        legend.text = element_text(size = 12),
        legend.position = "bottom",
        legend.title = element_blank(),
        strip.text.x = element_text(size = 12)) +
  facet_wrap(~ cap_macro)



#create new dfs with only period of interest
qt_less  <- RISP_QT_crises
written_less <- RISP_WRITTEN_crises

qt_less <- qt_less %>% filter(semester > "2004.2")
written_less <- written_less %>% filter(semester > "2004.2")


#calculate the proportion of the cateogry on the total for oral questions

qt_less <- qt_less %>% group_by(cap_macro, semester) %>% 
  mutate(prop = nQT.categSUM/TOTsemesterQT*100)


#calculate the proportion of the cateogry on the total for written questions

written_less <- written_less %>% group_by(cap_macro, semester) %>% 
  mutate(prop = nWRITTEN.categSUM/TOTsemesterWRITTEN*100)


#summary statistics of proportions

written_less %>% 
  filter(cap_macro == "9") %>% 
  summary(min = min(prop),
          mean = mean(prop),
          median = median(prop),
          max = max(prop))


qt_less %>% group_by(cap_macro) %>% 
  filter(cap_macro == "9") %>% 
  summary(min = min(prop),
          mean = mean(prop),
          median = median(prop),
          max = max(prop))



written_less %>% 
  filter(cap_macro == "3") %>% 
  summary(min = min(prop),
          mean = mean(prop),
          median = median(prop),
          max = max(prop))


qt_less %>% group_by(cap_macro) %>% 
  filter(cap_macro == "3") %>% 
  summary(min = min(prop),
          mean = mean(prop),
          median = median(prop),
          max = max(prop))



written_less %>% 
  filter(cap_macro == "1") %>% 
  summary(min = min(prop),
          mean = mean(prop),
          median = median(prop),
          max = max(prop))


qt_less %>% group_by(cap_macro) %>% 
  filter(cap_macro == "1") %>% 
  summary(min = min(prop),
          mean = mean(prop),
          median = median(prop),
          max = max(prop))




#Figure 3. Proportion of attention towards Economy, Health, and Immigration in oral and written questions (2005–2022).####

written_less %>% 
  filter(cap_macro == "1" | cap_macro == "3" | cap_macro == "9") %>% 
  ggplot(aes(x = as.character(semester), y = prop, group = as.character(cap_macro), colour = as.character(cap_macro))) +
  geom_line(linewidth = 0.7) +
  scale_color_manual(breaks = c("1", "3", "9"),
                     labels = c("Economy", "Health", "Immigration"),
                     values = c("black", "#67a9cf", "#ef8a62")) +
  scale_y_continuous(limits = c(0, 30),
                     breaks = c(0, 10, 20, 30)) +
  ggtitle("Written Questions") +
  labs(x = "Semester",
       y = "Proportion") +
  theme_classic() +
  theme(panel.grid.major.x = element_line(),
        panel.grid.minor.y = element_blank(),
        plot.title = element_text(face = "bold", size = 15),
        axis.text.y = element_text(size = 12),
        axis.text.x = element_text(size = 12, angle = 90, hjust = 1),
        axis.title.y = element_text(size = 14, margin = unit(c(0,3,0,0), "mm")),
        axis.title.x = element_text(size = 14, margin = unit(c(5,0,0,0), "mm")),
        legend.text = element_text(size = 12),
        legend.position = "bottom",
        legend.title = element_blank()) +
  guides(color = guide_legend(nrow = 1, byrow = TRUE)) -> plot_writtenPROP


qt_less %>% 
  filter(cap_macro == "1" | cap_macro == "3" | cap_macro == "9") %>% 
  ggplot(aes(x = as.character(semester), y = prop, group = as.character(cap_macro), colour = as.character(cap_macro))) +
  geom_line(linewidth = 0.7) +
  scale_color_manual(breaks = c("1", "3", "9"),
                     labels = c("Economy", "Health", "Immigration"),
                     values = c("black", "#67a9cf", "#ef8a62")) +
  scale_y_continuous(limits = c(0, 30),
                     breaks = c(0, 10, 20, 30)) +
  ggtitle("Question Time") +
  labs(x = "Semester",
       y = "Proportion") +
  theme_classic() +
  theme(panel.grid.major.x = element_line(),
        panel.grid.minor.y = element_blank(),
        plot.title = element_text(face = "bold", size = 15),
        axis.text.y = element_text(size = 12),
        axis.text.x = element_text(size = 12, angle = 90, hjust = 1),
        axis.title.y = element_text(size = 14, margin = unit(c(0,3,0,0), "mm")),
        axis.title.x = element_blank(),#margin = unit(c(5,0,0,0), "mm")),
        legend.text = element_blank(),
        legend.position = "none",
        legend.title = element_blank()) +
  guides(color = guide_legend(nrow = 1, byrow = TRUE)) -> plot_qtPROP


cowplot::plot_grid(plot_qtPROP, plot_writtenPROP, 
                   rel_heights = c(0.45,0.55), scale = 0.95, nrow = 2)


